package cn.wizzer.app.sys.modules.services.impl;

import cn.wizzer.app.sys.modules.models.SysSensitive;
import cn.wizzer.app.sys.modules.services.SysSensitiveService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.sensi.SensitiveFilter;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.ArrayUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;

import java.util.List;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = SysSensitiveService.class)
public class SysSensitiveServiceImpl extends BaseServiceImpl<SysSensitive> implements SysSensitiveService {

    public SysSensitiveServiceImpl(Dao dao) {
        super(dao);
    }

    public static SensitiveFilter SENSITIVE_FILTER = new SensitiveFilter();

    @Override
    public String filter(String sentence) {
        return SENSITIVE_FILTER.filter(sentence, '*');
    }

    @Override
    public void put(List<String> words) {
        for (String word : words) {
            SENSITIVE_FILTER.put(word);
        }
    }

    @Override
    public Pagination listPage(Integer pageNumber, int pageSize, String searchKeyword, String[] searchDate) {
        Sql sql = Sqls.create("select * from sys_sensitive $condition ");
        Sql countSql = Sqls.create("select count(1) from sys_sensitive $condition ");
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(searchKeyword)) {
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and(Cnd.exps("words", "like", searchKeyword)
                    .and("type", "like", searchKeyword));
        }
        if (ArrayUtils.isNotEmpty(searchDate)) {
            cnd.and("opAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("opAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        sql.setCondition(cnd);
        sql.appendSourceSql("order by opAt desc");
        countSql.setCondition(cnd);
        return this.listPage(pageNumber, pageSize, sql, countSql);
    }
}
